using System;
using System.Collections.Generic;
using System.Text;
using Task.Contracts;
using System.Net.Mail;
using System.Configuration;

namespace Task
{
    /// <summary>
    /// Email Task.
    /// </summary>
    public sealed class EmailTask : IEmailTask
    {
        #region IEmailTask Members

        /// <summary>
        /// Sends the email.
        /// </summary>
        /// <param name="email">The email.</param>
        /// <param name="body">The body.</param>
        /// <param name="subject">The subject.</param>
        public void SendEmail(string email, string body, string subject)
        {
            SmtpClient client = new SmtpClient(GetEmailServerName());
            client.UseDefaultCredentials = false;
            System.Net.NetworkCredential cred = new System.Net.NetworkCredential(
                GetEmailUser(), GetEmailPassword());
            client.Credentials = cred;

            MailAddress addressFrom = new MailAddress(GetEmailEmail(), GetEmailFrom());
            MailAddress addressTo = new MailAddress(email);

            MailMessage ms = new MailMessage(addressFrom, addressTo);

            ms.Subject = subject;
            ms.IsBodyHtml = false;

            ms.Body = body;

            client.Send(ms);
        }

        #endregion IEmailTask Members

        #region Private Methods

        /// <summary>
        /// Gets the email from.
        /// </summary>
        /// <returns></returns>
        private static string GetEmailFrom()
        {
            return ConfigurationManager.AppSettings["Mail_from"];
        }

        /// <summary>
        /// Gets the email user.
        /// </summary>
        /// <returns></returns>
        private static string GetEmailUser()
        {
            return ConfigurationManager.AppSettings["Mail_user"];
        }

        /// <summary>
        /// Gets the email password for mail.
        /// </summary>
        /// <returns></returns>
        private static string GetEmailPassword()
        {
            return ConfigurationManager.AppSettings["Mail_password"];
        }

        /// <summary>
        /// Gets the name of the email server.
        /// </summary>
        /// <returns></returns>
        private static string GetEmailServerName()
        {
            return ConfigurationManager.AppSettings["Mail_mailserver"];
        }

        /// <summary>
        /// Gets the email email.
        /// </summary>
        /// <returns></returns>
        private static string GetEmailEmail()
        {
            return ConfigurationManager.AppSettings["Mail_mail"];
        }

        #endregion Private Methods
    }
}
